package com.dq.service.evaluation.impl;

import com.dq.controller.in.EvaluateRecordQuery;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.EvaluateQuery;
import com.dq.domain.evaluation.EvaluationRecord;
import com.dq.domain.org.Org;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.evaluation.EvaluationRecordService;
import com.dq.service.org.OrgService;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;

/**
 * @author liuhui
 * @date 2019/11/22 0022 17:29:23
 */
@Service
public class EvaluationRecordServiceImpl extends BaseRepositories implements EvaluationRecordService {
    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private OrgService orgService;

    @Override
    public List<EvaluationRecord> findbyWhere(EvaluateQuery evaluateQuery) throws AdminException {
        StringBuilder hql = new StringBuilder("select u from EvaluationRecord u where u.deleted=false");
        Query q = createWhereHql(evaluateQuery, hql);
        q.setFirstResult((evaluateQuery.getPage() - 1) * evaluateQuery.getSize());
        q.setMaxResults(evaluateQuery.getSize());
        List<EvaluationRecord> evaluationRecordList = q.getResultList();
        return evaluationRecordList;
    }

    @Override
    public Long countbyWhere(EvaluateQuery evaluateQuery) throws AdminException {
        StringBuilder hql = new StringBuilder("select count(u) from EvaluationRecord u where u.deleted=false");
        Query q = createWhereHql(evaluateQuery, hql);
        return new Long((long) q.getResultList().get(0));
    }

    @Override
    public Vo list(EvaluateRecordQuery evaluateRecordQuery) throws AdminException {
        evaluateRecordQuery.format();
        //测试
       // evaluateRecordQuery.setOrgId(0L);
        Long orgId = evaluateRecordQuery.getOrgId();

        Map<String, Object> query = evaluateRecordQuery.parseMap();
        if (null != orgId && 0 != orgId) {
            query.put("subscribe_sysDept_id", evaluateRecordQuery.getOrgId());
            query.remove("orgId");
        } else {
            query.remove("orgId");
        }
        if (evaluateRecordQuery.getUserRealcName() != null && !"".equals(evaluateRecordQuery.getUserRealcName())) {
            query.put("subscribe_wxUser_userRealcName", evaluateRecordQuery.getUserRealcName());
            query.remove("userRealcName");
        }
        if (evaluateRecordQuery.getStaffUserRealcName() != null && !"".equals(evaluateRecordQuery.getStaffUserRealcName())) {
            query.put("subscribe_technicianUser_userRealcName", evaluateRecordQuery.getStaffUserRealcName());
            query.remove("staffUserRealcName");
        }
        List<EvaluationRecord> evaluationRecordList = dao.find(dao.query2Hql(EvaluationRecord.class, query), query);
        Long count = dao.count(dao.count2Hql(EvaluationRecord.class, query), query);
        PageHelper.startPage(evaluateRecordQuery.getPage(), evaluateRecordQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<EvaluationRecord.Vo>) VO.toVOCollection(evaluationRecordList));
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }

    public Query createWhereHql(EvaluateQuery evaluateQuery, StringBuilder hql) throws AdminException {
        if (null != evaluateQuery.getOrgId()) {
            hql.append(" and u.serviceRecord.sysDept =:org");
        } else {
            // 只显示负责门店信息
            String orgIdsIn = orgService.getIdListAllByUser();
            if (StringUtils.isNotBlank(orgIdsIn)) {
                hql.append(" and u.serviceRecord.sysDept in ").append(orgIdsIn);
            }
        }
        if (null != evaluateQuery.getBeginTime()) {
            hql.append(" and u.createTime >=:beginTime");
        }
        if (null != evaluateQuery.getEndTime()) {
            hql.append(" and u.createTime <=:endTime");
        }
        if (StringUtils.isNotEmpty(evaluateQuery.getWxUserName())) {
            hql.append(" and u.serviceRecord.wxUser.nickName like:wxNickName");
        }
        if (null != evaluateQuery.getBeginScore()) {
            hql.append(" and u.averageScore >=:beginScore");
        }
        if (null != evaluateQuery.getEndScore()) {
            hql.append(" and u.averageScore <=:endScore");
        }
        if (StringUtils.isNotEmpty(evaluateQuery.getStaffUserName())) {
            hql.append(" and u.serviceRecord.staffUser.nickName like:staNickName");
        }
        if (null != evaluateQuery.getStaffUserId()) {
            hql.append(" and u.serviceRecord.staffUser =:staffUser");
        }
        hql.append(" ORDER BY u.createTime desc");
        Query q = this.entityManager.createQuery(hql.toString());
        if (null != evaluateQuery.getOrgId()) {
            q.setParameter("org", dao.get(Org.class, evaluateQuery.getOrgId()));
        }
        if (null != evaluateQuery.getBeginTime()) {
            q.setParameter("beginTime", evaluateQuery.getBeginTime());
        }
        if (null != evaluateQuery.getEndTime()) {
            q.setParameter("endTime", evaluateQuery.getEndTime());
        }
        if (StringUtils.isNotEmpty(evaluateQuery.getWxUserName())) {
            q.setParameter("wxNickName", "%" + evaluateQuery.getWxUserName() + "%");
        }
        if (null != evaluateQuery.getBeginScore()) {
            q.setParameter("beginScore", evaluateQuery.getBeginScore());
        }
        if (null != evaluateQuery.getEndScore()) {
            q.setParameter("endScore", evaluateQuery.getEndScore());
        }
        if (StringUtils.isNotEmpty(evaluateQuery.getStaffUserName())) {
            q.setParameter("staNickName", "%" + evaluateQuery.getStaffUserName() + "%");
        }
        if (null != evaluateQuery.getStaffUserId()) {
            q.setParameter("staffUser", dao.get(SysUser.class, evaluateQuery.getStaffUserId()));
        }
        return q;

    }

}
